package controller;

import model.MySql;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintStream;

/**
 * Created by yvdedu.com on 2016/10/9.
 */
@WebServlet(urlPatterns = "/download")
public class download extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String book_id=req.getParameter("book_id");
        MySql mySql=new MySql();

        StringBuffer stringBuffer=mySql.DownLoad(Integer.parseInt(book_id),"C:\\Users\\yvdedu.com\\IdeaProjects\\小说\\book\\");
        resp.setContentType("multipart/form-data");
        resp.setHeader("Content-Disposition", "attachment;fileName="+mySql.getBookInfoById(Integer.parseInt(book_id)).getName()+".txt");
        PrintStream printStream=new PrintStream(resp.getOutputStream());
        printStream.print(stringBuffer);
    }
}
